<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable
                          @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <!-- <el-form-item label="父类id" prop="parentId">
                <el-input v-model="queryParams.parentId" placeholder="请输入父类id" clearable
                          @keyup.enter.native="handleQuery"/>
            </el-form-item> -->
            <el-form-item label="描述" prop="description">
                <el-input v-model="queryParams.description" placeholder="请输入描述" clearable
                          @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" size="small" @click="handleAdd"
                           v-hasPermi="['shm:category:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" size="small" :disabled="single" @click="handleUpdate"
                           v-hasPermi="['shm:category:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" size="small" :disabled="multiple" @click="handleDelete"
                           v-hasPermi="['shm:category:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" size="small" @click="handleExport"
                           v-hasPermi="['shm:category:export']">导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange"> -->
        <el-table :data="treeData" class="treeData" row-key="id" :default-expand-all="false"
                  :tree-props="{ children: 'children' }" style="width: 100%">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="分类名称" align="left" prop="name"/>
            <el-table-column label="图标" align="left" prop="icon"/>
            <el-table-column label="排序" align="left" prop="sort"/>
            <el-table-column label="描述" align="left" prop="description"/>
            <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" class="button-success" icon="Plus" size="small"
                               @click="handleAdd(scope.row)">添加下级节点
                    </el-button>
                    <el-button size="samll" type="text" icon="Edit" @click="handleUpdate(scope.row)"
                               v-hasPermi="['market:category:edit']">修改
                    </el-button>
                    <el-button size="samll" type="text" icon="Delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['market:category:remove']">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加职位 -->
        <!-- <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
                <el-form ref="dataForm" :model="position" label-width="9.375rem" size="small" style="padding-right: 2.5rem">
                  <el-form-item label="上级职位" v-if="position.id === ''">
                    <el-input v-model="position.parentName" :disabled="true" />
                  </el-form-item>
                  <el-form-item label="职位名称" prop="name">
                    <el-input v-model="position.name" />
                  </el-form-item>
                  <el-form-item label="职位描述" prop="description">
                    <el-input v-model="position.description" />
                  </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
                  <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
                </span>
              </el-dialog> -->

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>

        <!-- 添加或修改商品分类对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入分类名称"/>
                </el-form-item>
                <!-- <el-form-item label="父类id" prop="parentId">
                    <el-input v-model="form.parentId" placeholder="请输入父类id" />
                </el-form-item> -->
                <el-form-item label="图标" prop="icon">
                    <el-input v-model="form.icon" placeholder="请输入图标"/>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" placeholder="请输入排序"/>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="form.description" placeholder="请输入描述"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listCategory,
    getCategory,
    delCategory,
    addCategory,
    updateCategory,
    getProductCategoryTree,
} from "@/api/market/category";

// const defaultForm = {
//   id: "",
//   parentId: "",
//   name: "",
//   description: "",
// };
export default {
    name: "Category",
    data() {
        return {
            dataForm: {},
            //是否可编辑
            typeDisabled: false,
            //对话框标题
            dialogVisible: false,
            //初始化为空数组
            treeData: [],
            // 默认props属性
            defaultProps: {
                children: "children",
                label: "label",
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 商品分类表格数据
            categoryList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                isDeleted: null,
                name: null,
                parentId: null,
                icon: null,
                sort: null,
                description: null,
            },
            data: [
                {
                    label: "一级 1",
                    children: [
                        {
                            label: "二级 1-1",
                            children: [
                                {
                                    label: "三级 1-1-1",
                                },
                            ],
                        },
                    ],
                },
            ],
            // 表单参数
            form: {
                parentId: 0,
                status: 0,
            },
            // 表单校验
            rules: {
                createTime: [
                    {required: true, message: "创建时间不能为空", trigger: "blur"},
                ],
                updateTime: [
                    {required: true, message: "更新时间不能为空", trigger: "blur"},
                ],
                name: [
                    {required: true, message: "分类名称不能为空", trigger: "blur"},
                ],
                parentId: [
                    {required: true, message: "父类id不能为空", trigger: "blur"},
                ],
                icon: [{required: true, message: "图标不能为空", trigger: "blur"}],
                sort: [{required: true, message: "排序不能为空", trigger: "blur"}],
            },
        };
    },
    created() {
        this.getProductCategoryTree();
    },
    methods: {
        /** 查询商品分类列表 */
        getList() {
            this.loading = true;
            listCategory(this.queryParams).then((response) => {
                this.categoryList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getProductCategoryTree() {
            getProductCategoryTree(this.queryParams).then((res) => {
                console.log(this.queryParams);
                // 将获取到的数据赋值给treeData
                this.treeData = res.data;
                // console.log("getProductCategoryTree:" + JSON.stringify(res));
                // this.areaItemVoOptions = res.data;
            });
        },
        //弹出添加或更新的表单
        // add(row) {
        //   // debugger
        //   this.typeDisabled = false;
        //   this.dialogTitle = "添加下级节点";
        //   this.dialogVisible = true;

        //   this.position = Object.assign({}, defaultForm);
        //   this.position.id = "";
        //   if (row) {
        //     this.position.parentId = row.id;
        //     this.position.parentName = row.name;
        //   } else {
        //     this.dialogTitle = "添加根职位";
        //     this.position.parentId = 0;
        //     this.typeDisabled = true;
        //   }
        // },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                createTime: null,
                updateTime: null,
                isDeleted: null,
                name: null,
                parentId: 0,
                icon: null,
                sort: null,
                description: null,
                status: 0
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getProductCategoryTree();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd(row = null) {
            if (row != null) {
                this.form.parentId = row.id;
            } else {
                this.form.parentId = 0;
            }
            // this.reset();
            this.open = true;
            this.title = "添加商品分类";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getCategory(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改商品分类";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCategory(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getProductCategoryTree();
                            this.reset();
                        });
                    } else {
                        addCategory(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getProductCategoryTree();
                            this.reset();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除商品分类编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delCategory(ids);
                })
                .then(() => {
                    this.getProductCategoryTree();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {
                });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "shm/category/export",
                {
                    ...this.queryParams,
                },
                `category_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
